import Vue from 'vue'
import Router from 'vue-router'
import { constantRouterMap } from '@/config/router.config'
import { generateIndexRouter } from '@/utils/util'

Vue.use(Router)

const router = new Router({
  mode: 'history',
  base: '/',
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRouterMap
})

// 全局路由(无需嵌套)
const globalRoutes = [
  // ... 现有代码
]

// 主框架路由
const mainRoutes = {
  // ... 现有代码
}

// 添加余料管理和半料管理路由到菜单中
// 查找物料管理菜单
export function addMaterialRoutes(asyncRouterMap) {
  if (!asyncRouterMap || !asyncRouterMap.length) return asyncRouterMap

  // 遍历一级菜单
  for (let i = 0; i < asyncRouterMap.length; i++) {
    const menu = asyncRouterMap[i]

    // 如果有子菜单，递归查找
    if (menu.children && menu.children.length) {
      // 查找仓库管理菜单
      if (menu.path === '/wms') {
        // 在仓库管理菜单下查找物料管理菜单
        for (let j = 0; j < menu.children.length; j++) {
          const submenu = menu.children[j]

          // 找到物料管理菜单
          if (submenu.path === '/wms/material') {
            // 确保子菜单数组存在
            if (!submenu.children) {
              submenu.children = []
            }

            // 添加余料管理路由
            const surplusRoute = {
              path: '/wms/material/surplus',
              name: 'WmsMaterialSurplus',
              component: () => import('@/views/wms/material/SurplusList'),
              meta: {
                title: '余料管理',
                keepAlive: true,
                icon: 'profile'
              }
            }

            // 添加半料管理路由
            const semiRoute = {
              path: '/wms/material/semi',
              name: 'WmsMaterialSemi',
              component: () => import('@/views/wms/material/SemiList'),
              meta: {
                title: '半料管理',
                keepAlive: true,
                icon: 'block'
              }
            }

            // 检查路由是否已存在
            const hasSurplus = submenu.children.some(route => route.path === surplusRoute.path)
            const hasSemi = submenu.children.some(route => route.path === semiRoute.path)

            // 如果不存在则添加
            if (!hasSurplus) {
              submenu.children.push(surplusRoute)
            }
            if (!hasSemi) {
              submenu.children.push(semiRoute)
            }

            break
          }

          // 找到成品库存菜单，添加成品入库路由
          if (submenu.path === '/wms/finished-goods') {
            // 确保子菜单数组存在
            if (!submenu.children) {
              submenu.children = []
            }

            // 添加成品入库管理路由
            const inboundRoute = {
              path: '/wms/finished-goods/inbound',
              name: 'WmsFinishedGoodsInbound',
              component: () => import('@/views/wms/finishedGoods/InboundList'),
              meta: {
                title: '成品入库',
                keepAlive: true,
                icon: 'import'
              }
            }

            // 检查路由是否已存在
            const hasInbound = submenu.children.some(route => route.path === inboundRoute.path)

            // 如果不存在则添加
            if (!hasInbound) {
              submenu.children.push(inboundRoute)
            }

            break
          }
        }
        break
      }

      // 递归处理其他子菜单
      menu.children = addMaterialRoutes(menu.children)
    }
  }

  return asyncRouterMap
}

export default router
